package com.lcy.algorithm.Leecode;

import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;

/**
 * 举例
 * 思想：用map装规则
 * 遇到左括弧压入栈，遇到右括弧弹栈，如果不匹配返回错误
 */
public class 判断符号是否成双出现 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HashMap<Character, Character> mappings = new HashMap<Character, Character>();
        mappings.put(')', '(');
        mappings.put('}', '{');
        mappings.put(']', '[');
        while (scanner.hasNextLine()) {
            String origin = scanner.nextLine();
            char[] charArray = origin.toCharArray();
            boolean isValid = true;
            Stack<Character> stack = new Stack<>();
            for (char c : charArray) {
                if (mappings.containsKey(c)) {
                    char topElement = stack.empty() ? '#' : stack.pop();
                    if (topElement != mappings.get(c)) {
                        isValid = false;
                        break;
                    }
                } else {
                    stack.push(c);
                }
            }
            System.out.println(isValid);
        }

    }
}
